﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DLTN.Data;
using DLTN.Entities;

public partial class Lien_He_Van_Chuyen : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(! IsPostBack)
        {
            Process();
        }
    }

    public void Process()
    {
        string str = Request.QueryString["MaXe"];
        this.lblCode.Text = str + " - " + GetTenXeVanChuyen(str);
        this.ImageMaXe.ImageUrl = "~/Upload/VanChuyen/" + GetImageVanChuyen(str);
    }
    public string GetImageVanChuyen(object Id)
    {
        int s = Convert.ToInt32(Id.ToString());
        VanChuyen ent_VanChuyen = DataRepository.VanChuyenProvider.GetById(s);
        return ent_VanChuyen.AnhDaiDien;
    }

    public string GetTenXeVanChuyen(object Id)
    {
        int s = Convert.ToInt32(Id.ToString());
        VanChuyen ent_VanChuyen = DataRepository.VanChuyenProvider.GetById(s);
        return ent_VanChuyen.TenXe;
    }

    protected void btnRegister_Click(object sender, EventArgs e)
    {
        CaptchaControl.ValidateCaptcha(this.txtCapCha.Text);
        if (CaptchaControl.UserValidated)
        {
            bool updateStatus = false;
            try
            {
                LienHeVanChuyen ent_LienHeVanChuyen = new LienHeVanChuyen();
                ent_LienHeVanChuyen.IdVanChuyen = Convert.ToInt32(Request.QueryString["MaXe"].ToString());
                ent_LienHeVanChuyen.HoTen = this.txtName.Text.Trim();
                ent_LienHeVanChuyen.NgaySinh = Convert.ToDateTime((this.txtBirthDay.Text));
                ent_LienHeVanChuyen.DiaChi = this.txtAddress.Text.Trim();
                ent_LienHeVanChuyen.DienThoai = this.txtTel.Text.Trim();
                ent_LienHeVanChuyen.ThuDienTu = this.txtEmail.Text.Trim();
                ent_LienHeVanChuyen.SoLuong = Convert.ToInt32(this.txtQuantity.Text.ToString());
                ent_LienHeVanChuyen.NoiDung = this.txtContent.Text.Trim();
                ent_LienHeVanChuyen.TrangThai = false;
                ent_LienHeVanChuyen.NgayCapNhat = Common.GetServerDateTime();

                updateStatus = DataRepository.LienHeVanChuyenProvider.Insert(ent_LienHeVanChuyen);

                if (updateStatus)
                {
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.LHVC_DANGKY_THANHCONG, Constants.LHVC_DANGKY_THANHCONG, true);
                    RedirectTo("~/");

                }
                else
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.LHVC_DANGKY_THATBAI, true);
            }
            catch (Exception ex)
            {
                Common.WriteLog("Lien-He-Van-Chuyen.aspx.cs", "btnRegister_Click", ex.ToString());
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.LHVC_DANGKY_THATBAI, true);
            }
        }
        else
        {
            ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.LH_CAPCHA_THATBAI, true);
        }
    }

    private void RedirectTo(string url)
    {
        string redirectURL = Page.ResolveClientUrl(url);
        string script = String.Format("window.location = '{0}';", redirectURL);
        ScriptManager.RegisterStartupScript(this, typeof(Page), "RedirectTo", script, true);
    }
}